import axios from "axios";
import store from '../store/store';

// 已经配置了前端代理，就不再需要使用 imooc 的接口了
// axios.defaults.baseURL = "http://apis.imooc.com/api/"

axios.interceptors.request.use(config => {
    // config.params = { ...config.params, icode: 'xxxx' }
    store.commit('setLoading', true);
    store.commit('setError', { status: false, message: ''});
    return config;
}, error => {
    return Promise.reject(error);
});

axios.interceptors.response.use(response => {
    store.commit('setLoading', false);
    return response;
}, e => {
    const { error } = e.response.data;;
    store.commit('setError', { status: true, message: error});
    store.commit('setLoading', false);
    return Promise.reject(e);
});

export default axios;